package ctrl

import (
	"fmt"
	"im/model"
	"im/service"
	"im/util"
	"math/rand"
	"net/http"
)
var userService service.UserService

func UserLogin(writer http.ResponseWriter, request *http.Request) {
	//io.WriteString(writer,"hello word")
	request.ParseForm()
	mobile:=request.Form.Get("mobile")
	passwd:=request.Form.Get("passwd")
	user,err:=userService.Login(mobile,passwd)
	if err!=nil{
		util.RespFail(writer,err.Error())
	}else{
		util.RespOk(writer,user,"")
	}
}

func UserRegister(writer http.ResponseWriter, request *http.Request) {
	request.ParseForm()
	mobile:=request.Form.Get("mobile")
	plainpwd:=request.Form.Get("passwd")
	nickname:=fmt.Sprintf("user%06d",rand.Int31())
	avatar:=request.Form.Get("passwd")
	sex:=model.SEX_UNKONW
	user,err:=userService.Register(mobile,plainpwd,nickname,avatar,sex)
	if	err!=nil{
		util.RespFail(writer,err.Error())
	}else{
		util.RespOk(writer,user,"")
	}
}
